<template>
  <div>
    <!-- 标题 -->
    <header class="charts-header">
      <span>{{title}}</span>
      <span>
        <i style="vertical-align:text-bottom;color:grey" class="el-icon-info"></i>
      </span>
    </header>

    <!-- 内容 -->
    <div class="content">
      <span class="content-main">{{num}}</span>
      <span class="content-percent">
        {{percent}}
        <i :class="icon"></i>
      </span>
    </div>

    <!-- 图表 -->
    <div class="lineCharts" ref="charts"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'lineCharts',
  // title：标题  num：数量  percent：百分比  flag：上涨/下跌 lineData：折线图的数据
  props: ['title', 'num', 'percent', 'flag', 'lineData'],
  computed: {
    icon () {
      return this.flag == 'up' ? 'el-icon-caret-top' : 'el-icon-caret-bottom'
    }
  },
  mounted () {
    const lineCharts = echarts.init(this.$refs.charts)
    lineCharts.setOption({
      xAxis: {
        // 隐藏坐标轴
        show: false,
        type: 'category'
      },
      yAxis: {
        show: false
      },
      grid: {
        top: 0,
        left: 0,
        right: 0,
        bottom: 0
      },
      series: [
        {
          type: 'line',
          data: this.lineData,
          // 光滑度
          smooth: .6,
          // 拐点透明图
          itemStyle: {
            opacity: 0
          },
          // 线条样式
          lineStyle: {
            color: '#9ed4e8'
          },
          // 折线图填充样式
          areaStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0, color: '#9ed4e8' // 0% 处的颜色
              }, {
                offset: 1, color: '#fff' // 100% 处的颜色
              }],
              global: false // 缺省为 false
            }
          }
        }
      ]
    })
  }
}
</script>

<style scoped>
.charts-header {
  display: flex;
  justify-content: space-between;
}

.el-icon-caret-top {
  color: red;
}

.el-icon-caret-bottom {
  color: green;
}

.content {
  margin: 20px 0;
}

.content-main {
  font-size: 30px;
  margin-right: 30px;
}

.content-percent {
  color: grey;
  font-size: 16px;
}

.lineCharts {
  width: 100%;
  height: 50px;
}
</style>
